﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class Inspectors : Form
    {
        int userId = -1;
        public Inspectors(int id,Form m)
        {
            fo = m;
            userId = id;
            this.MdiParent = m;
            InitializeComponent();
        }
        public bool is_loaded = false;
        ModelContainer ctx = new ModelContainer();
        Form fo = null;
        private void Inspectors_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            User user = ctx.Users.Single(u => u.Id == userId);
            foreach (var Item in user.Role.Permissions)
            {
                switch (Item.Name)
                {
                    case "INSPECTORS_DELETE":
                        btn_Delete.Visible = true;
                        break;
                    case "INSPECTORS_ADD_UPDATE":
                        btn_Add_Edit.Visible = true;
                        break;
                    default:
                        break;
                }
            }
            inspectorBindingSource.DataSource = ctx.Inspectors.ToList();
            lstBoxInpector.DataSource = inspectorBindingSource;
            lstBoxInpector.DisplayMember = "Name";
            lstBoxInpector.ValueMember = "Id";

        }

        private void btn_Add_Click()
        {
            string text = txb_name.Text;
            text.Trim();
            if (ctx.Inspectors.Where(c => c.Name == text).Count() > 0)
            {
                MessageBox.Show("الاسم موجود مسبقاً");
            }
            else
            {
                try
                {
                    if (text.Length > 0)
                    {
                        Inspector s = new Inspector();
                        s.Name = text;
                        s.InsertDate = DateTime.Now;
                        s.InsertUId = userId;
                        ctx.Inspectors.Add(s);
                        ctx.SaveChanges();
                        inspectorBindingSource.DataSource = ctx.Inspectors.ToList();
                        lstBoxInpector.DataSource = inspectorBindingSource;
                        lstBoxInpector.DisplayMember = "Name";
                        lstBoxInpector.ValueMember = "Id";
                        txb_name.Text = String.Empty;
                    }
                    else
                    {
                        MessageBox.Show("يرجى اضافة اسم صالح وليس فارغاً");
                    }
                }
                catch (Exception)
                {

                }
            }
        }

        private void btn_dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstBoxInpector.SelectedItems.Count; i++)
            {
                if (lstBoxInpector.SelectedItems[i] != null)
                {
                    Inspector inspector = (Inspector)lstBoxInpector.SelectedItems[i];
                    if (ctx.Worthies.Where(c => c.InspectorId == inspector.Id).Count() > 0)
                    {
                        MessageBox.Show("لا يمكن حذف كشاف /مشرف" + " ( " + inspector.Name + " ) " + "مسندة له مهمة  ");
                    }
                    else
                    {
                        ctx.Inspectors.Remove(ctx.Inspectors.Single(c => c.Id == inspector.Id));
                        ctx.SaveChanges();
                    }
                }
            }
            inspectorBindingSource.DataSource = ctx.Inspectors.ToList();
            lstBoxInpector.DataSource = inspectorBindingSource;
            lstBoxInpector.DisplayMember = "Name";
            lstBoxInpector.ValueMember = "Id";
        }

        private void btn_Edit_Click()
        {
            
            if (lstBoxInpector.SelectedValue!=null)
            {
                int iid = (int)lstBoxInpector.SelectedValue;
                if (iid > 0)
                {
                    Inspector s = ctx.Inspectors.Single(c => c.Id == iid);
                    s.Name = txb_name.Text;
                    s.UpdateDate = DateTime.Now;
                    s.UpdateUId = userId;
                    ctx.SaveChanges();
                    txb_name.Text = String.Empty;
                    inspectorBindingSource.DataSource = ctx.Inspectors.ToList();
                    lstBoxInpector.DataSource = inspectorBindingSource;
                    lstBoxInpector.DisplayMember = "Name";
                    lstBoxInpector.ValueMember = "Id";
                }
            }

        }

        private void txb_name_TextChanged(object sender, EventArgs e)
        {
            if (txb_name.Text.Length > 0)
            {
                btn_Add_Edit.Visible = true;
                btn_Delete.Enabled = false;
            }

        }

        private void lstBoxInpector_CellDoubleClick(object sender, EventArgs e)
        {
            if (lstBoxInpector.SelectedValue != null)
            {
                string name = lstBoxInpector.Text;
                txb_name.Text = name;
                btn_Add_Edit.Text = "تعديل";
                btn_Delete.Enabled = true;
                txb_name.Focus();
            }
        }

        private void Inspectors_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void Inspectors_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

        private void lstBoxInpector_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
                    if (ctx.Inspectors.Where(c => c.Name == lstBoxInpector.Text).Count() > 0)
                    {
                        int id;
                        if (int.TryParse(lstBoxInpector.SelectedValue.ToString(),out id))
                        {
                            Inspector ins = ctx.Inspectors.Single(c => c.Id == id);
                            ins.Name = lstBoxInpector.Text;
                            ins.UpdateDate = DateTime.Now;
                            ins.UpdateUId = userId;
                        }
                    else
                    {
                        if (lstBoxInpector.SelectedValue != null)
                        {
                            Inspector i = new Inspector();
                            i.Name = lstBoxInpector.Text.ToString();
                            i.InsertDate = DateTime.Now;
                            i.InsertUId = userId;
                            ctx.Inspectors.Add(i);
                        }
                }
                ctx.SaveChanges();
            }
        }

        private void lstBoxInpector_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lstBoxInpector.SelectedValue != null)
            {
                int iid = (int)lstBoxInpector.SelectedValue;
                if (iid > 0)
                {
                    Inspector ins = ctx.Inspectors.Single(i => i.Id == iid);
                    txb_name.Text = ins.Name;
                    btn_Add_Edit.Text = "تعديل";
                    btn_Delete.Enabled = true;
                    txb_name.Focus();
                }
            }
        }

        private void btn_Add_Edit_Click(object sender, EventArgs e)
        {
            if (btn_Add_Edit.Text == "إضافة")
            {
                btn_Add_Click();
            }
            else
            {
                btn_Edit_Click();
            }
        }

        private void lstBoxInpector_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_Add_Edit.Text = "إضافة";
            txb_name.Text = string.Empty;
        }
    }
    
}
